package org.wangblue.concrete_iterator;

import org.wangblue.concrete_aggregate.ConcreteAggregate;
import org.wangblue.iterator.Iterator;

/**
 * @Description: 具体迭代器类，继承Iterator
 * @Author: WangBlue
 * @Create: 2024-09-23 10:10
 * @Version: 1.0
 **/

public class ConcreteIterator extends Iterator {
    private final ConcreteAggregate aggregate;
    private int currentIndex = 0;

    public ConcreteIterator(ConcreteAggregate aggregate) {
        this.aggregate = aggregate;
    }
    @Override
    public Object fist() {
        return aggregate.getCurrentItem(0);
    }

    @Override
    public Object next() {
        Object ret = null;
        currentIndex++;
        if (currentIndex < aggregate.getCount()) {
            ret = aggregate.getCurrentItem(currentIndex);
        }
        return ret;
    }

    @Override
    public boolean hasNext() {
        return currentIndex >= aggregate.getCount();
    }

    // 返回当前聚集对象
    @Override
    public Object currentItem(){
        return  aggregate.getCurrentItem(currentIndex);
    }
}
